import pandas as pd
import pymongo, datetime
import tushare as ts
import matplotlib as mpl
import matplotlib.pyplot as plt

conn = pymongo.MongoClient()


def get_sharp(fund_code):
    c = conn.stks.fund_codes.find_one({'code': fund_code})
    if c is None:
        return
    fund = {'name': c['name'], 'fund_id': c['_id'], 'code': fund_code}
    df_fund = pd.DataFrame(
        list(
            conn.stks.fund_daily_values.find({
                'fund_id': fund['fund_id'],
                'date': {
                    '$gte': datetime.datetime.strptime('2017-01-01',
                                                       '%Y-%m-%d'),
                    '$lte': datetime.datetime.now()
                }
            })))
    df_fund.sort_values('date', ascending=True, inplace=True)
    df_fund['change'] = df_fund['net_asset_value'].pct_change()

    annual_return = (df_fund['net_asset_value'].tail(1).values[0] /
                     df_fund['net_asset_value'].head(1).values[0])**(
                         250 / df_fund.shape[0]) - 1
    lost_free_return = 0.04
    sharp = (annual_return -
             lost_free_return) / df_fund['change'].describe().std()
    print(fund['name'] + ' Sharp=', round(sharp * 100, 2), '%')


funds = ['519195', '110022', '003095', '001617', '001195', '502010', '217027']
for f in funds:
    get_sharp(f)
